package com.xinshujia.controller;

import com.google.gson.Gson;
import com.xinshujia.dto.AlarmRecordDTO;
import com.xinshujia.entity.AlarmRecordEntity;
import com.xinshujia.response.RespResult;
import com.xinshujia.service.AlarmRecordService;
import com.xinshujia.utils.GsonUtil;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-07-19 14:25       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 * 预警事件日志控制类
 */
@RestController
@RequestMapping("alarm")
public class AlarmRecordController {
    private final AlarmRecordService alarmRecordService;

    public AlarmRecordController(AlarmRecordService alarmRecordService) {
        this.alarmRecordService = alarmRecordService;
    }


    /**
     * 预警操作
     *
     * @param deviceGuid  设备Id
     * @param operateType 操作类型（0：关闭预警， 1： 开启预警， 2： 清除预警）
     */
    @GetMapping("alarmOperate")
    public RespResult alarmOperate(@RequestParam("deviceGuid") String deviceGuid, @RequestParam("operateType") Integer operateType) {
        Boolean flag = alarmRecordService.alarmOperate(deviceGuid, operateType);
        return flag ? RespResult.OK() : RespResult.Fail();
    }


    /**
     * 预警排名
     */
    @GetMapping("getAlarmOrder")
    public RespResult getAlarmOrder() {
        List<AlarmRecordDTO> alarmRecordDTOS = alarmRecordService.getAlarmOrder();
        List<AlarmRecordDTO> list = alarmRecordDTOS.stream().filter(alarmRecordDTO -> alarmRecordDTO.getAlarmCount() > 0).toList();
        Gson gson = GsonUtil.googleGson();
        return RespResult.OK(gson.toJson(list));
    }

    /**
     * 根据Device_guid查询预警记录
     */
    @GetMapping("getAlarmRecordsByDeviceGuid")
    public RespResult getAlarmRecordsByDeviceGuid(@RequestParam("deviceGuid") String deviceGuid){
        List<AlarmRecordDTO> alarmRecordDTOS = alarmRecordService.getAlarmRecordsByDeviceGuid(deviceGuid);
        Gson gson = GsonUtil.googleGson();
        return RespResult.OK(gson.toJson(alarmRecordDTOS));
    }

    /**
     * 根据Id查询
     */
    @GetMapping("getAlarmRecordById")
    public RespResult getAlarmRecordById(@RequestParam("id") Long id){
        AlarmRecordDTO alarmRecordDTO = alarmRecordService.getAlarmRecordById(id);
        Gson gson = GsonUtil.googleGson();
        return RespResult.OK(gson.toJson(alarmRecordDTO));
    }

    /**
     * 预警记录处理
     */
    @PostMapping("handlerAlarmRecord")
    public RespResult handlerAlarmRecord(@RequestBody AlarmRecordEntity alarmRecordEntity){
        Boolean flag = alarmRecordService.handlerAlarmRecord(alarmRecordEntity);
        return flag ? RespResult.OK() : RespResult.Fail();
    }

}
